package com.xinkao.example.controller;

import java.io.IOException;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinkao.example.config.annotation.SysLog;
import com.xinkao.example.config.base.BaseController;
import com.xinkao.example.domain.excel.ExampleInputErrEntity;
import com.xinkao.example.domain.excel.ExampleOutputEntity;
import com.xinkao.example.service.UploadFileService;
import com.xinkao.example.service.impl.MinioServiceImpl;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.Constants;
import com.xinkao.example.util.constants.ErrorEnum;
import com.xinkao.example.util.easyexcel.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xinkao.example.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import com.xinkao.example.domain.entity.ExampleEntity;
import com.xinkao.example.service.ExampleService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 示例表
 * @Author lililiang
 * @Date 2021-08-20 17:38:54
 */
@RestController
@Api(tags = "示例表")
@RequestMapping("/example")
public class ExampleController extends BaseController{
    @Resource
    private ExampleService exampleService;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private UploadFileService uploadFileService;
    /**
     * 分页查询
     */
    @SysLog(module = "示例模块", operate = "分页查询")
//    @RequiresPermissions("example:list")
    @ApiOperation(value = "分页查询", response = ExampleEntity.class)
    @PostMapping("/list")
    public JSONObject list(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "pageNum, pageRow");
        return exampleService.list4Page(requestJson);
    }


    /**
     * 列表查询
     */
    @SysLog(module = "示例模块", operate = "列表查询")
    @ApiOperation(value = "列表查询", response = ExampleEntity.class)
    @PostMapping("/listByCondition")
    public JSONObject listByCondition(@RequestBody JSONObject requestJson){
        return exampleService.listByCondition(requestJson);
    }


    /**
     * 查询详情
     */
    @SysLog(module = "示例模块", operate = "查询详情")
//    @RequiresPermissions("example:list")
    @ApiOperation(value = "查询详情", response = ExampleEntity.class)
    @PostMapping("/info/{id}")
    public JSONObject info(@PathVariable("id") String id){
        return exampleService.queryOneById(id);
    }


    /**
     * 新增
     */
    @SysLog(module = "示例模块", operate = "新增")
//    @RequiresPermissions("example:save")
    @ApiOperation(value = "新增", response = JSONObject.class)
    @PostMapping("/save")
    public JSONObject save(@RequestBody ExampleEntity entity){
        entity.setCreateBy(getUserId());
        return exampleService.insertOrEdit(entity);
    }

    /**
     * 修改
     */
    @SysLog(module = "示例模块", operate = "修改")
//    @RequiresPermissions("example:edit")
    @ApiOperation(value = "修改", response = JSONObject.class)
    @PostMapping("/edit")
    public JSONObject edit(@RequestBody ExampleEntity entity){
        entity.setUpdateBy(getUserId());
        return exampleService.insertOrEdit(entity);
    }


    /**
     * 删除/批量删除
     */
    @SysLog(module = "示例模块", operate = "删除")
//    @RequiresPermissions("example:delete")
    @ApiOperation(value = "删除/批量删除", response = JSONObject.class)
    @PostMapping("/delete")
    public JSONObject delete(@RequestBody List<String> ids){
        return exampleService.deleteLogic(ids, getUserId());
    }

    /**
     * 下载模板
     * @return
     */
    @SysLog(module = "示例模块", operate = "下载模板")
    @GetMapping(value = "/downloadTemplate", produces = {"application/json;charset=UTF-8"})
    public JSONObject downloadTemplate() {
        //直接返回EXCEL模板静态地址
        return CommonUtil.successJson(Constants.TEMPLATES_EXCEL_ADDRESS);
    }

    /**
     * 导入excel
     */
    @SysLog(module = "示例模块", operate = "导入excel")
    @ApiOperation(value = "导入excel", response = JSONObject.class)
    @PostMapping("/input")
    public JSONObject input(MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return CommonUtil.errorJson(ErrorEnum.E_20015);
        }
        List<ExampleInputErrEntity> list = EasyExcelUtil.readExcel(file, ExampleInputErrEntity.class);
        return exampleService.inputExcel(list);
    }

    /**
     * 下载导入excel错误信息
     */
    @SysLog(module = "示例模块", operate = "下载导入excel错误信息")
    @PostMapping(value = "/errorExport")
    public void errorExport(HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {
        CommonUtil.hasAllRequired(requestJson, "paramData");
        List<ExampleInputErrEntity> list = JSON.parseArray(
                JSONObject.toJSONString(redisUtils.get(requestJson.getString("paramData"), List.class)),
                ExampleInputErrEntity.class);
        EasyExcelUtil.wirteExcel(response, "学生信息导入错误", "学生信息", list, ExampleInputErrEntity.class);
    }

    /**
     * 导出excel
     */
    @SysLog(module = "示例模块", operate = "导出excel")
    @ApiOperation(value = "导出excel", response = JSONObject.class)
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        List<ExampleEntity> list = exampleService.list(new LambdaQueryWrapper<ExampleEntity>().eq(ExampleEntity::getDeleteStatus, 0));
        EasyExcelUtil.wirteExcel(response, "导出", "导出测试",
                JSON.parseArray(JSON.toJSONString(list), ExampleOutputEntity.class),
                ExampleOutputEntity.class);
    }

    /**
     * 上传附件
     */
    @SysLog(module = "示例模块", operate = "上传附件")
    @PostMapping("/uploadFile")
    public JSONObject uploadFile(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return CommonUtil.errorJson(ErrorEnum.E_20015);
        }
        return uploadFileService.upload("/test/", file);
    }
}
